package com.right.health.app.ui.me;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;

import com.magic.basic.activity.BaseActivity;
import com.magic.basic.utils.CollectionUtil;
import com.magic.basic.utils.ToastUtil;
import com.magic.basic.widget.refresh.OnLastItemVisibleListener;
import com.magic.basic.widget.refresh.SwipeListRefreshLayout;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.app.App;
import com.right.health.app.bean.VisitCardResponse;
import com.right.health.app.constants.BroadCastKey;
import com.right.health.app.net.UserApi;
import com.right.health.app.ui.me.adapter.MeVisitCardAdapter;

import java.util.ArrayList;
import java.util.List;

import static com.right.health.app.constants.BroadCastCode.REFRESH_VISITCARD;

/*
 * Created by hopeliao on 2019-09-29
 *
 * 就诊卡列表
 */
public class MeVisitCardActivity extends BaseActivity implements View.OnClickListener, OnLastItemVisibleListener, SwipeRefreshLayout.OnRefreshListener, OnHttpListener {
    private static final int HTTP_CODE_REFRESH = 1;
    private static final int HTTP_CODE_LOAD_MORE = 2;

    private static final String EXTRA_KEY = "memberId";
    private static final String EXTRA_ISCHOOSE = "extra:isChoose";

    private MeVisitCardAdapter mAdapter;

    private SwipeListRefreshLayout mRefreshView;

    private int mPageNo = 1;

    private UserApi mUserApi;

    public List<VisitCardResponse.VisitCardInfo> mDataSource = new ArrayList<>();

    private String mMemberid;

    private ImageButton mAddIb;

    private boolean mIsChoose;

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.me_visit_card);
    }

    @Override
    public void findView() {
        mRefreshView = (SwipeListRefreshLayout) findViewById(R.id.refresh_view);
        mRefreshView.setLastItemVisibleListener(this);
        mRefreshView.setOnRefreshListener(this);

        mAdapter = new MeVisitCardAdapter(this);
        ListView listView = (ListView) findViewById(R.id.listview);
        listView.setAdapter(mAdapter);
        listView.setEmptyView(findViewById(R.id.empty_view));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                VisitCardResponse.VisitCardInfo info = (VisitCardResponse.VisitCardInfo)parent.getItemAtPosition(position);
                if(info != null) {
                   if(mIsChoose) {
                       Intent intent = new Intent();
                       Bundle bundle = new Bundle();
                       bundle.putParcelable(BroadCastKey.DATA, info);
                       intent.putExtras(bundle);
                       setResult(RESULT_OK, intent);
                       finish();
                   }
                }
            }
        });

        mAddIb = (ImageButton) findViewById(R.id.ib_add);
        mAddIb.setOnClickListener(this);
        mAddIb.setClickable(false);
        findViewById(R.id.tv_goto_add).setOnClickListener(this);
    }

    @Override
    public void initialize() {
        mUserApi = new UserApi();
        mUserApi.setListener(this);

        mMemberid = App.getInstance().getCurrentMember().id;
        Bundle bundle = getIntent().getExtras();
        if(bundle != null) {
            if(bundle.containsKey(EXTRA_ISCHOOSE)) {
                mIsChoose = bundle.getBoolean(EXTRA_ISCHOOSE, false);
            }
            if(bundle.containsKey(EXTRA_KEY)) {
                mMemberid = bundle.getString(EXTRA_KEY);
            }
        }

        onReceiveBroadcast(REFRESH_VISITCARD, null);
    }


    @Override
    public void onReceiveBroadcast(int intent, Bundle bundle) {
        super.onReceiveBroadcast(intent, bundle);
        if(intent == REFRESH_VISITCARD) {
            mRefreshView.setRefreshing(true);
            onRefresh();
        }
    }

    public static void applyActivity(Activity act, String memberid, boolean isChoose, int requestCode) {
        Intent intent = new Intent(act, MeVisitCardActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString(EXTRA_KEY, memberid);
        bundle.putBoolean(EXTRA_ISCHOOSE, isChoose);
        intent.putExtras(bundle);

        act.startActivityForResult(intent, requestCode);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_goto_add:
            case R.id.ib_add:
                if(!TextUtils.isEmpty(mMemberid)) {
                    MeVisitCardAddActivity.applyActivity(this, mMemberid);
                }
                break;
        }
    }

    @Override
    public void onRefresh() {
        mPageNo = 1;
        mUserApi.addRequestCode(HTTP_CODE_REFRESH);
        mUserApi.getVisitCard(mMemberid, mPageNo);
    }

    @Override
    public void onLastItemVisible() {
        mPageNo++;
        mUserApi.addRequestCode(HTTP_CODE_LOAD_MORE);
        mUserApi.getVisitCard(mMemberid, mPageNo);
    }

    @Override
    public void onResponse(Request request, Response response) {
        mRefreshView.onRefreshComplete();

        if(response.isResponseFail()) {
            ToastUtil.show(this, response.error);
        } else {
            if(response.targetData instanceof VisitCardResponse) {
                VisitCardResponse visitCardResponse = response.cast(VisitCardResponse.class);

                if(request.requestCode == HTTP_CODE_REFRESH) {
                    mDataSource.clear();
                }

                mDataSource.addAll(visitCardResponse.patientidcards);

                mAddIb.setClickable(CollectionUtil.isEmpty(mDataSource));

                if(visitCardResponse.page != null) {
                    mRefreshView.setCanLoadMore(!visitCardResponse.page.lastPage);
                }

                mAdapter.setDataSource(mDataSource);
            }
        }
    }
}
